package com.jcj.house;

import com.jcj.framework.Game;
import com.jcj.framework.Input.TouchEvent;
import com.jcj.framework.gl.Camera2D;
import com.jcj.framework.gl.SpriteBatcher;
import com.jcj.framework.gl.TextureRegion;
import com.jcj.framework.impl.GLScreen;
import com.jcj.framework.math.OverlapTester;
import com.jcj.framework.math.Rectangle;
import com.jcj.framework.math.Vector2;
import java.util.List;
import javax.microedition.khronos.opengles.GL10;

public class PantallaJuego extends GLScreen {
    Camera2D guiCam;
    SpriteBatcher batcher;
    Rectangle pauseBounds;
    Rectangle cactusBounds;
    Rectangle pianoBounds;
    Rectangle enrredaderaBounds;
    Rectangle frutaBounds;
    Rectangle izqBounds;
    Rectangle derBounds;
    Vector2 touchPoint;

    public PantallaJuego(Game game) {
        super(game);
        guiCam = new Camera2D(glGraphics, 320, 480);
        batcher = new SpriteBatcher(glGraphics, 100);
        pauseBounds = new Rectangle(145, 480-2-96, 29, 96);
        enrredaderaBounds = new Rectangle(268, 480-140-105, 50, 105);
        pianoBounds = new Rectangle(273, 480-375-102, 46, 102);
        frutaBounds = new Rectangle(1, 480-377-101, 47, 101);
        cactusBounds = new Rectangle(1, 480-137-105, 105, 105);
        izqBounds = new Rectangle(1, 480-263-111, 45, 111);
        derBounds = new Rectangle(268, 480-263-108, 47, 108);
        
        touchPoint = new Vector2();
    }
    
    @Override
    public void update(float deltaTime) {
        List<TouchEvent> touchEvents = game.getInput().getTouchEvents();
        game.getInput().getKeyEvents();
        int len = touchEvents.size();
        for (int i = 0; i < len; i ++) {
            TouchEvent event = touchEvents.get(i);
            if (event.type == TouchEvent.TOUCH_UP) {
                touchPoint.set(event.x, event.y);
                guiCam.touchToWorld(touchPoint);

                if (OverlapTester.pointInRectangle(pauseBounds, touchPoint)) {
                    game.setScreen(new PauseScreen(game));
                    return;
                }

                if (OverlapTester.pointInRectangle(pianoBounds, touchPoint)) {
                	game.setScreen(new GameoverScreen(game));
                    //Assets.playSound(Assets.clickSound);
                    //game.setScreen(new HighscoreScreen(game));
                    return;
                }

                if (OverlapTester.pointInRectangle(cactusBounds, touchPoint)) {
                	game.setScreen(new GameoverScreen(game));
                    //Assets.playSound(Assets.clickSound);
                    //game.setScreen(new HelpScreen(game));
                    return;
                }
                
                if (OverlapTester.pointInRectangle(derBounds, touchPoint)) {
                    //Assets.playSound(Assets.clickSound);
                    //game.setScreen(new HelpScreen(game));
                    return;
                }
                
                if (OverlapTester.pointInRectangle(izqBounds, touchPoint)) {
                    //Assets.playSound(Assets.clickSound);
                    //game.setScreen(new HelpScreen(game));
                    return;
                }
                
                if (OverlapTester.pointInRectangle(enrredaderaBounds, touchPoint)) {
                    //Assets.playSound(Assets.clickSound);
                    //game.setScreen(new HelpScreen(game));
                    return;
                }

                if (OverlapTester.pointInRectangle(frutaBounds, touchPoint)) {
                    //Assets.playSound(Assets.clickSound);
                    //Settings.soundEnabled = !Settings.soundEnabled;
                    //if (Settings.soundEnabled)
                    //    Assets.music.play();
                    //else
                    //    Assets.music.pause();
                }
            }
        }
    }

    @Override
    public void present(float deltaTime) {
        GL10 gl = glGraphics.getGL();
        gl.glClear(GL10.GL_COLOR_BUFFER_BIT);
        guiCam.setViewportAndMatrices();

        gl.glEnable(GL10.GL_TEXTURE_2D);
        
       /* batcher.beginBatch(Assets.background);
        batcher.drawSprite(160, 240, 320, 480, Assets.backgroundRegion);
        batcher.endBatch();
        */
        
        gl.glEnable(GL10.GL_BLEND);
        gl.glBlendFunc(GL10.GL_SRC_ALPHA, GL10.GL_ONE_MINUS_SRC_ALPHA);
        
        batcher.beginBatch(Assets.items);
        
        batcher.drawSprite(64,480-97,25,86,Assets.hero);
        //batcher.drawSprite(101,480-97,25,86,Assets.hero);
        //batcher.drawSprite(101,480-97,24,69,Assets.fruta);
        //batcher.drawSprite(140,480-97,23,71,Assets.cactus);
        //batcher.drawSprite(178,480-97,60,156,Assets.piano);

        
        //batcher.drawSprite(214,480-97,25,86,Assets.hero);
        //batcher.drawSprite(252,480-97,25,86,Assets.hero);
        batcher.endBatch();

        gl.glDisable(GL10.GL_BLEND);
    }

    @Override
    public void pause() {
        Settings.save(game.getFileIO());
    }

    @Override
    public void resume() {

    }

    @Override
    public void dispose() {
        
    }
}
